package com.group.three.controller;

import com.github.pagehelper.PageInfo;
import com.group.three.pojo.*;
import com.group.three.pojo.vo.JsonResult;
import com.group.three.service.*;
import com.group.three.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/film")
public class IndexController {
    @Autowired
    private SelectListVideoService selectListVideoService;
    @Autowired
    private VideoTagService videoTagService;
    @Autowired
    private PreviewService previewService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private FavoriteVideosService favoriteVideosService;
    //index
    @RequestMapping("/index")
    public ModelAndView indexPage(){
        ModelAndView mav = new ModelAndView();
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        PageInfo<Videos> videos = selectListVideoService.selectListVideo(null,1,12);
        List<VideoTag> videoTags = videoTagService.selectByVideoTag(null);
        mav.addObject("videoTags",videoTags);
        mav.addObject("videos",videos);
        mav.addObject("year",year);
        List<PreviewVideos> previewVideos = previewService.selectAllPreview(1);
        mav.addObject("previewVideos",previewVideos);
        mav.setViewName("/1-index-animation/animation");
        mav.setViewName("/2-movie/index");
        return mav;
    }
    @RequestMapping("/changePage")
    @ResponseBody
    public JsonResult changePage(Integer page,Integer size){
        PageInfo<Videos> videosPageInfo = selectListVideoService.selectListVideo(null, page, size);

        return new JsonResult("200","success",videosPageInfo);
    }
    //play
    @RequestMapping("/player/{videoId}/{movieName}")
    public ModelAndView selectVideoByVideoId(@PathVariable("videoId")Long videoId,
                                             @PathVariable("movieName")String movieName){
        ModelAndView mav = new ModelAndView();
        //评论
        PageInfo<Comment> homePageInfo = commentService.selectHomeCommentList(movieName, 1, 5);
        mav.addObject("homePageInfo",homePageInfo);
//收藏
        List<FavoriteVideos> favoriteVideos = favoriteVideosService.selectFavoriteAll();
        System.out.println("6666666666"+favoriteVideos);
        mav.addObject("favoriteVideos",favoriteVideos);
        List<Videos> videos = selectListVideoService.selectListVideo(videoId,1);
        //播放量
        Integer number = videos.get(0).getVideoNumber();
        number = number + 1;
        selectListVideoService.updatePlayNumber(number,videoId);

        //标签
        Long videoTag = videos.get(0).getVideoTag();
        List<VideoTag> videoTags = videoTagService.selectByVideoTag(videoTag);
        //时长
        Long videoLength = videos.get(0).getVideoLength();
        String s = TimeUtils.formatTime(videoLength);
        mav.addObject("length",s);
        //日期
        Date videoOntime = videos.get(0).getVideoOntime();
        String timeByDate = TimeUtils.getTimeByDate(videoOntime);
        mav.addObject("timeByDate",timeByDate);

        mav.addObject("playMovieTag",videoTags);
        mav.addObject("playMovie",videos);
        mav.setViewName("/2-movie/play");
        return mav;
    }
    //预告片
    @RequestMapping("/prePlayer/{videoId}")
    public ModelAndView prePlayer(@PathVariable("videoId")Long videoId){
        ModelAndView mav = new ModelAndView();
        List<Videos> videos = selectListVideoService.selectListVideo(videoId,0);
        if(null == videos){
            mav.setViewName("/4-404/404");
            return mav;
        }else{
            Long videoTag = videos.get(0).getVideoTag();
            List<VideoTag> videoTags = videoTagService.selectByVideoTag(videoTag);
            mav.addObject("playMovieTag",videoTags);
            mav.addObject("playMovie",videos);
            mav.setViewName("/2-movie/play");
            return mav;}
    }
    //Search
    private String SearchName;
    @RequestMapping("/doSearch/{searchName}")
    public ModelAndView doSearch(@PathVariable("searchName") String searchName){
        SearchName = searchName;
        ModelAndView modelAndView = new ModelAndView();
        PageInfo<Videos> videos = selectListVideoService.selectListVideo(null,searchName,1,10);
        modelAndView.addObject("searchVideo",videos);
        modelAndView.addObject("searchName",searchName);
        modelAndView.setViewName("/2-movie/search");
        return modelAndView;
    }
    @RequestMapping("/changeSearchPage")
    @ResponseBody
    public JsonResult changeSearchPage(Integer page,Integer size){
        PageInfo<Videos> videosPageInfo = selectListVideoService.selectListVideo(null,SearchName, page, size);

        return new JsonResult("200","success",videosPageInfo);
    }
    @RequestMapping("/doSearchByName/{searchName}")
    @ResponseBody
    public JsonResult doSearchByName(@PathVariable("searchName") String searchName){
        SearchName = searchName;
        PageInfo<Videos> videos = selectListVideoService.selectListVideo(null,searchName,1,10);
        return new JsonResult("200",searchName,videos);
    }
    @RequestMapping("/preview")
    public ModelAndView getPreview(){
        ModelAndView modelAndView = new ModelAndView();
        List<PreviewVideos> previewVideos = previewService.selectAllPreview(1);
        modelAndView.addObject("previewVideos",previewVideos);
        modelAndView.setViewName("/1-index-animation/animation");
        return modelAndView;
    }
    @RequestMapping("/getVideoByTag/{tag}")
    @ResponseBody
    public JsonResult getVideoByTag(@PathVariable("tag")String tag){
        Long integer = videoTagService.selectTagIdByTagName(tag);
        PageInfo<Videos> videos = selectListVideoService.selectselectVideoByTagId(integer, 1, 12);
        videos.getList();
        return new JsonResult("200","msg",videos);
    }

    @RequestMapping("/changeVideoByTag")
    @ResponseBody
    public JsonResult changeVideoByTag(String tag,Integer page,Integer size){
        Long integer = videoTagService.selectTagIdByTagName(tag);
        PageInfo<Videos> videosPageInfo = selectListVideoService.selectListVideo(integer, page, size);
        return new JsonResult("200","success",videosPageInfo);
    }

    @RequestMapping("/getVideoByStar/{number}")
    @ResponseBody
    public JsonResult getVideoByStar(@PathVariable("number") Integer number){
        PageInfo<Videos> videosPageInfo = selectListVideoService.selectVideoByStar(number, 1, 12);
        return new JsonResult("200","success",videosPageInfo);
    }

    @RequestMapping("/changeVideoByStar")
    @ResponseBody
    public JsonResult changeVideoByStar(Integer star,Integer page,Integer size){

        PageInfo<Videos> videosPageInfo = selectListVideoService.selectVideoByStar(star, page, size);
        return new JsonResult("200","success",videosPageInfo);
    }
    //按年
    @RequestMapping("/getVideoByYear/{number}")
    @ResponseBody
    public JsonResult getVideoByYear(@PathVariable("number") Integer number){
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        String onYear =null;
        String endYear=null;
        if(number < year-3){
            endYear =number +"-12-31";
        }else{
            onYear=number+"-1-1";
            endYear=number+"-12-31";
        }
        PageInfo<Videos> videosPageInfo = selectListVideoService.selectVideoByYear(onYear, endYear, 1, 12);

        return new JsonResult("200","success",videosPageInfo);
    }
    @RequestMapping("/changeVideoByYear")
    @ResponseBody
    public JsonResult changeVideoByYear(Integer Year,Integer page,Integer size){
        Calendar cal = Calendar.getInstance();
        int year1 = cal.get(Calendar.YEAR);
        String onYear =null;
        String endYear=null;
        if(Year < year1-3){
            endYear =Year +"-12-31";
        }else{
            onYear=Year+"-1-1";
            endYear=Year+"-12-31";
        }
        PageInfo<Videos> videosPageInfo = selectListVideoService.selectVideoByYear(onYear, endYear, page, size);

        return new JsonResult("200","success",videosPageInfo);
    }

    //按播放排序
    @RequestMapping("/getVideoByPlay/{number}")
    @ResponseBody
    public JsonResult getVideoByPlay(@PathVariable("number")Integer number){
        PageInfo<Videos> videosPageInfo = selectListVideoService.selectVideoByPlay(number, 1, 12);
        return new JsonResult("200","success",videosPageInfo);
    }

    @RequestMapping("/changeVideoByPlay")
    @ResponseBody
    public JsonResult changeVideoByPlay(Integer playNmu,Integer page,Integer size){
        PageInfo<Videos> videosPageInfo = selectListVideoService.selectVideoByPlay(playNmu, page, size);
        return new JsonResult("200","success",videosPageInfo);
    }

    //按评论排序
    @RequestMapping("/getCommentRank/{number}")
    @ResponseBody
    public JsonResult getCommentRank(@PathVariable("number")String number){
        PageInfo<Videos> videosPageInfo = selectListVideoService.selectVideoByCom(number, 1, 12);
        return new JsonResult("200","success",videosPageInfo);
    }

    @RequestMapping("/doGetVideoByCom")
    @ResponseBody
    public JsonResult doGetVideoByCom(String commentNum,Integer page,Integer size){
        PageInfo<Videos> videosPageInfo = selectListVideoService.selectVideoByCom(commentNum, page, size);
        return new JsonResult("200","success",videosPageInfo);
    }
}
